********
* DATA *
********

* Open ESS1-9
use "\Data\ESS1-8e01.dta", clear 
append using "\Data\ESS9e01_1.dta"

*************
* RECODINGS *
*************

* Attitudes
recode imsmetn imdfetn impcntr (7/9=.)
recode imbgeco imueclt imwbcnt (77/99=.)
recode stfdem (77/99=.)
recode stfgov (77/99=.)
drop isco08-occm14b
alpha imbgeco imueclt imwbcnt, gen(nativism)
replace nativism = 10-nativism
alpha trstprl trstlgl trstplc trstplt trstprt, std item gen(poltrust)
recode imwbcnt (0/4=1) (5/10=0), gen(nativist)
recode imdfetn (4=1) (1 2 3=0), gen(no_immigr)
recode imueclt (0/4=1) (5/10=0), gen(nativist2)
alpha polcmpl poldcs, gen(efficacy)

* Controls
recode gndr (2=0) (9=.), gen(female)
recode eisced (1 2=1 "Low") (3 4=2 "Middle") (5/7=3 "High") (0 55 77 88 99=.), gen(edu3)
recode agea (999=.), gen(age)

* Cases
recode essround (1=2002) (2=2004) (3=2006) (4=2008) (5=2010) (6=2012) (7=2014) (8=2016) (9=2018), gen(year)
encode cntry, gen(country)
recode country (1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 28 29 30=1) (nonmissing=0), gen(keep)
recode country (1 2 7 8 11 12 13 14 20 23 24 29=1) (nonmissing=0), gen(selection)
keep if keep==1

* Inclusion
recode year (2002 2004 2006 2018=1) (nonmissing=0) if cntry=="AT", gen(incl_at)
recode year (2002 2004 2006 2008 2010=1) (nonmissing=0) if cntry=="DK", gen(incl_dk)
recode year (2016=1) (nonmissing=0) if cntry=="FI", gen(incl_fi)
recode year (2002 2004 2018=1) (nonmissing=0) if cntry=="IT", gen(incl_it)
recode year (2010=1) (nonmissing=0) if cntry=="NL", gen(incl_nl)
recode year (2014 2016 2018=1) (nonmissing=0) if cntry=="NO", gen(incl_no)
egen inclusion = rowmean(incl_*)

* Vote choice
recode vote (2=0) (1=1) (3/9=.), gen(voted)
rename vote voteX
gen vote=.
{
replace vote = 3 if prtvtat==3
replace vote = 1 if prtvtat==1 | prtvtat==2 | prtvtat==5
replace vote = 3 if prtvtaat==3
replace vote = 1 if prtvtaat==1 | prtvtaat==2 | prtvtaat==6
replace vote = 3 if prtvtbe==8
replace vote = 1 if prtvtbe==3 | prtvtbe==14 | prtvtbe==5 | prtvtbe==9 | prtvtbe==2 | prtvtbe==12 | prtvtbe==13
replace vote = 3 if prtvtabe==7 
replace vote = 1 if prtvtabe==2 | prtvtabe==3 | prtvtabe==9 | prtvtabe==5 | prtvtabe==13 | prtvtabe==6 | prtvtabe==8 | prtvtabe==12
replace vote = 3 if prtvtbbe==7 
replace vote = 1 if prtvtbbe==2 | prtvtbbe==9 | prtvtbbe==5 | prtvtbbe==13 | prtvtbbe==8 | prtvtbbe==12
replace vote = 3 if prtvtcbe==7 
replace vote = 1 if prtvtcbe==2 | prtvtcbe==3 | prtvtcbe==9 | prtvtcbe==5 | prtvtcbe==13 | prtvtcbe==8 | prtvtcbe==12
replace vote = 3 if prtvtch==4
replace vote = 1 if prtvtch==1 | prtvtch==2 | prtvtch==3 | prtvtch==5 | prtvtch==7 | prtvtch==8
replace vote = 3 if prtvtach==4 
replace vote = 1 if prtvtach==1 | prtvtach==2 | prtvtach==3 | prtvtach==5 | prtvtach==7 | prtvtach==8
replace vote = 3 if prtvtbch==4
replace vote = 1 if prtvtbch==1 | prtvtbch==2 | prtvtbch==3 | prtvtbch==5 | prtvtbch==6 | prtvtbch==12
replace vote = 2 if prtvtcy==1
replace vote = 1 if prtvtcy==2 | prtvtcy==3 | prtvtcy==7
replace vote = 3 if prtvde1==6
replace vote = 2 if prtvde1==5
replace vote = 1 if prtvde1==1 | prtvde1==2 | prtvde1==4
replace vote = 3 if prtvade1==6 | prtvade1==7
replace vote = 2 if prtvade1==5
replace vote = 1 if prtvade1==1 | prtvade1==2 | prtvade1==4
replace vote = 3 if prtvbde1==6 | prtvbde1==7
replace vote = 2 if prtvbde1==5
replace vote = 1 if prtvbde1==1 | prtvbde1==2 | prtvbde1==4
replace vote = 3 if prtvcde1==6 | prtvcde1==7
replace vote = 2 if prtvcde1==5
replace vote = 1 if prtvcde1==1 | prtvcde1==2 | prtvcde1==4
replace vote = 3 if prtvde2==6
replace vote = 2 if prtvde2==5
replace vote = 1 if prtvde2==1 | prtvde2==2 | prtvde2==4
replace vote = 3 if prtvade2==6 | prtvade2==7
replace vote = 2 if prtvade2==5
replace vote = 1 if prtvade2==1 | prtvade2==2 | prtvade2==4
replace vote = 3 if prtvbde2==6 | prtvbde2==7
replace vote = 2 if prtvbde2==5
replace vote = 1 if prtvbde2==1 | prtvbde2==2 | prtvbde2==4
replace vote = 3 if prtvcde2==6 | prtvcde2==7
replace vote = 2 if prtvcde2==5
replace vote = 1 if prtvcde2==1 | prtvcde2==2 | prtvcde2==4
replace vote = 3 if prtvtdk==6
replace vote = 2 if prtvtdk==5 | prtvtdk==10
replace vote = 1 if prtvtdk==1 | prtvtdk==2 | prtvtdk==3 | prtvtdk==4 | prtvtdk==7 | prtvtdk==8
replace vote = 3 if prtvtadk==6
replace vote = 2 if prtvtadk==5 | prtvtadk==10
replace vote = 1 if prtvtadk==1 | prtvtadk==2 | prtvtadk==3 | prtvtadk==4 | prtvtadk==7 | prtvtadk==8
replace vote = 3 if prtvtbdk==5
replace vote = 2 if prtvtbdk==4 | prtvtbdk==9
replace vote = 1 if prtvtbdk==1 | prtvtbdk==2 | prtvtbdk==3 | prtvtbdk==6 | prtvtbdk==7 | prtvtbdk==8
replace vote = 3 if prtvtfi==5
replace vote = 2 if prtvtfi==10 
replace vote = 1 if prtvtfi==1 | prtvtfi==3 | prtvtfi==6 | prtvtfi==9
replace vote = 3 if prtvtafi==5
replace vote = 2 if prtvtafi==9 
replace vote = 1 if prtvtafi==1 | prtvtafi==3 | prtvtafi==6 | prtvtafi==8
replace vote = 3 if prtvtbfi==5
replace vote = 2 if prtvtbfi==15
replace vote = 1 if prtvtbfi==1 | prtvtbfi==3 | prtvtbfi==6 | prtvtbfi==14
replace vote = 3 if prtvtfr==3
replace vote = 2 if prtvtfr==4 | prtvtfr==5 | prtvtfr==9
replace vote = 1 if prtvtfr==2 | prtvtfr==10 | prtvtfr==11 | prtvtfr==12 | prtvtfr==13
replace vote = 3 if prtvtafr==3 
replace vote = 2 if prtvtafr==4 | prtvtafr==5 | prtvtafr==9
replace vote = 1 if prtvtafr==2 | prtvtafr==10 | prtvtafr==11 | prtvtafr==12 | prtvtafr==13
replace vote = 3 if prtvtbfr==2
replace vote = 2 if prtvtbfr==3 | prtvtbfr==4 | prtvtbfr==7
replace vote = 1 if prtvtbfr==6 | prtvtbfr==8 | prtvtbfr==10 | prtvtbfr==11 
replace vote = 1 if prtvtgb==1 | prtvtgb==2 | prtvtgb==3 | prtvtgb==11 | prtvtgb==12 | prtvtgb==14 | prtvtgb==15
replace vote = 3 if prtvtagb==7 | prtvtagb==8
replace vote = 1 if prtvtagb==1 | prtvtagb==2 | prtvtagb==3 | prtvtagb==11 | prtvtagb==12 | prtvtagb==14 | prtvtagb==15
replace vote = 2 if prtvtgr==3 | prtvtgr==4
replace vote = 1 if prtvtgr==1 | prtvtgr==2 
replace vote = 3 if prtvtagr==6
replace vote = 2 if prtvtagr==3 | prtvtagr==4
replace vote = 1 if prtvtagr==1 | prtvtagr==2 
replace vote = 3 if prtvtbgr==5
replace vote = 2 if prtvtbgr==3 | prtvtbgr==4
replace vote = 1 if prtvtbgr==1 | prtvtbgr==2
replace vote = 3 if prtvtcgr==4
replace vote = 2 if prtvtcgr==3 | prtvtcgr==5
replace vote = 1 if prtvtcgr==1 | prtvtcgr==2
replace vote = 2 if prtvthu==6
replace vote = 1 if prtvthu==2 | prtvthu==5 | prtvthu==7
replace vote = 3 if prtvtahu==3
replace vote = 1 if prtvtahu==1 | prtvtahu==2 | prtvtahu==4 | prtvtahu==6 
replace vote = 3 if prtvtbhu==3
replace vote = 1 if prtvtbhu==1 | prtvtbhu==2 | prtvtbhu==4 | prtvtbhu==6
replace vote = 3 if prtvtchu==11
replace vote = 1 if prtvtchu==1 | prtvtchu==2 | prtvtchu==4 | prtvtchu==6
replace vote = 3 if prtvtit==9 
replace vote = 2 if prtvtit==3 | prtvtit==7
replace vote = 1 if prtvtit==1 | prtvtit==2 | prtvtit==8 | prtvtit==10 | prtvtit==12
replace vote = 3 if prtvtait==9
replace vote = 2 if prtvtait==3 | prtvtait==7
replace vote = 1 if prtvtait==1 | prtvtait==2 | prtvtait==8 | prtvtait==10 | prtvtait==12
replace vote = 3 if prtvtnl==4
replace vote = 2 if prtvtnl==7
replace vote = 1 if prtvtnl==1 | prtvtnl==2 | prtvtnl==3 | prtvtnl==5 | prtvtnl==8
replace vote = 3 if prtvtanl==4
replace vote = 2 if prtvtanl==7
replace vote = 1 if prtvtanl==1 | prtvtanl==2 | prtvtanl==3 | prtvtanl==5 | prtvtanl==8
replace vote = 3 if prtvtbnl==4
replace vote = 2 if prtvtbnl==7
replace vote = 1 if prtvtbnl==1 | prtvtbnl==2 | prtvtbnl==3 | prtvtbnl==5 | prtvtbnl==8
replace vote = 3 if prtvtcnl==11 // LPF too small, start PVV
replace vote = 2 if prtvtcnl==7
replace vote = 1 if prtvtcnl==1 | prtvtcnl==2 | prtvtcnl==3 | prtvtcnl==5 | prtvtcnl==8
replace vote = 3 if prtvtdnl==3
replace vote = 2 if prtvtdnl==5
replace vote = 1 if prtvtdnl==1 | prtvtdnl==2 | prtvtdnl==4 | prtvtdnl==6 | prtvtdnl==8
replace vote = 2 if prtvtno==1 | prtvtno==2
replace vote = 1 if prtvtno==3 | prtvtno==4 | prtvtno==5 | prtvtno==7 | prtvtno==9
replace vote = 3 if prtvtno==8
replace vote = 2 if prtvtano==1 | prtvtano==2
replace vote = 1 if prtvtano==3 | prtvtano==4 | prtvtano==5 | prtvtano==7 | prtvtano==9
replace vote = 3 if prtvtano==8
replace vote = 2 if prtvtpt==1 | prtvtpt==5
replace vote = 1 if prtvtpt==2 | prtvtpt==10 | prtvtpt==11
replace vote = 2 if prtvtapt==1 | prtvtapt==3 | prtvtapt==4
replace vote = 1 if prtvtapt==2 | prtvtapt==10 | prtvtapt==11
replace vote = 2 if prtvtbpt==1 | prtvtbpt==4
replace vote = 1 if prtvtbpt==2 | prtvtbpt==3 | prtvtbpt==10 | prtvtbpt==11
replace vote = 2 if prtvtse==7
replace vote = 1 if prtvtse==1 | prtvtse==2 | prtvtse==3 | prtvtse==5 | prtvtse==6
replace vote = 3 if prtvtase==10
replace vote = 2 if prtvtase==7
replace vote = 1 if prtvtase==1 | prtvtase==2 | prtvtase==3 | prtvtase==5 | prtvtase==6
replace vote = 2 if prtvtsk==7
replace vote = 1 if prtvtsk==1 | prtvtsk==2 | prtvtsk==3 | prtvtsk==5 | prtvtsk==6
replace vote = 3 if prtvtask==6
replace vote = 1 if prtvtask==1 | prtvtask==2 | prtvtask==3 | prtvtask==5
replace vote = 3 if prtvtbsk==1
replace vote = 1 if prtvtbsk==2 | prtvtbsk==3 | prtvtbsk==5 | prtvtbsk==6
replace vote = 3 if prtvtcbe==7
replace vote = 1 if prtvtcbe==2 | prtvtcbe==5 | prtvtcbe==6 | prtvtcbe==8 | prtvtcbe==12 | prtvtcbe==13
replace vote = 3 if prtvtcbg==3
replace vote = 1 if prtvtcbg==1 | prtvtcbg==2
replace vote = 3 if prtvtdch==1
replace vote = 1 if prtvtdch==2 | prtvtdch==3 | prtvtdch==4
replace vote = 3 if prtvdde1==6 | prtvdde1==7
replace vote = 2 if prtvdde1==5
replace vote = 1 if prtvdde1==1 | prtvdde1==2 | prtvdde1==4
replace vote = 3 if prtvdde2==6 | prtvdde2==7
replace vote = 2 if prtvdde2==5
replace vote = 1 if prtvdde2==1 | prtvdde2==2 | prtvdde2==4
replace vote = 3 if prtvtcdk==5
replace vote = 2 if prtvtcdk==4 | prtvtcdk==9
replace vote = 1 if prtvtcdk==1 | prtvtcdk==2 | prtvtcdk==3 | prtvtcdk==6 | prtvtcdk==7 | prtvtcdk==8
replace vote = 3 if prtvtcfi==4
replace vote = 2 if prtvtcfi==14 
replace vote = 1 if prtvtcfi==1 | prtvtcfi==2 | prtvtcfi==5 | prtvtcfi==13
replace vote = 1 if prtvtgb==1 | prtvtgb==2 | prtvtgb==3 | prtvtgb==11 | prtvtgb==12 | prtvtgb==14 | prtvtgb==15
replace vote = 3 if prtvtdhu==4
replace vote = 2 if prtvtdhu==10
replace vote = 1 if prtvtdhu==3 | prtvtdhu==6 | prtvtdhu==8 | prtvtdhu==9 | prtvtdhu==13
replace vote = 2 if prtvtaie==7
replace vote = 1 if prtvtaie==1 | prtvtaie==2 | prtvtaie==5
replace vote = 3 if prtvtenl==3
replace vote = 2 if prtvtenl==5
replace vote = 1 if prtvtenl==1 | prtvtenl==2 | prtvtenl==4 | prtvtenl==6 | prtvtenl==8
replace vote = 3 if prtvtano==8
replace vote = 2 if prtvtano==1 | prtvtano==2
replace vote = 1 if prtvtano==3 | prtvtano==4 | prtvtano==5 | prtvtano==7
replace vote = 2 if prtvtbpt==1
replace vote = 1 if prtvtbpt==2 | prtvtbpt==10 | prtvtbpt==11
replace vote = 3 if prtvtbse==10
replace vote = 2 if prtvtbse==7
replace vote = 1 if prtvtbse==2 | prtvtbse==3 | prtvtbse==5 | prtvtbse==6
replace vote = 3 if prtvtdsi==10
replace vote = 1 if prtvtdsi==2 | prtvtdsi==3 | prtvtdsi==4 | prtvtdsi==5 | prtvtdsi==6 | prtvtdsi==7 | prtvtdsi==8 | prtvtdsi==9
replace vote = 3 if prtvtcru==3
replace vote = 2 if prtvtcru==2 | prtvtcru==11 
replace vote = 1 if prtvtcru==1 | prtvtcru==6 
replace vote=3 if prtvtbat==3 | prtvtbat==4
replace vote=3 if prtvtcbe==7
replace vote=3 if prtvtech==1
replace vote=3 if prtvtcdk==5
replace vote=3 if prtvtcfi==4
replace vote=3 if prtvtcfr==2
replace vote=3 if prtvtbgb==7
replace vote=3 if prtvtehu==2
replace vote=3 if prtvtfnl==3
replace vote=3 if prtvtbno==8
replace vote=3 if prtvtbse==10
replace vote=3 if prtvede1==6 | prtvede1==8
replace vote=3 if prtvtbat==3 |  prtvtbat==4
replace vote=3 if prtvtcbe==7 |  prtvtcbe==11
replace vote=3 if prtvtcfr==2
replace vote=3 if prtvtbgb==7
replace vote=3 if prtvtfnl==3
replace vote=3 if prtvtbno==8
replace vote=3 if prtvtbse==10
replace vote=3 if prtvtfch==1
replace vote=3 if prtvtdfi==4
replace vote=3 if prtvede1==6 | prtvede1==8
replace vote=3 if prtvtbit==9
replace vote=3 if prtvtcat==3
replace vote=3 if prtvtdbe==7 | prtvtdbe==11
replace vote=3 if prtvtdfr==11
replace vote=3 if prtvtcgb==7
replace vote=3 if prtvtgnl==3
replace vote=3 if prtvtdfi==4
replace vote=3 if prtvtbno==8
replace vote=3 if prtvede1==6 | prtvede1==8
replace vote=3 if prtvtcit==9
}
gen RRvote=0
replace RRvote=1 if vote==3
replace RRvote=. if voted==0
replace RRvote=. if voted==.
replace RRvote=. if voted==.a
replace RRvote=. if voted==.b
replace RRvote=. if voted==.c
replace RRvote=. if voted==.d
bys country: egen RRincountry = mean(RRvote)
bys country year: egen partysize = mean(RRvote)
egen case = group(country year)

* Winner/loser
capture drop winlose* date 
*use "C:\Users\ehartev1\Dropbox\Inclusion paper\Data\ESS1-8e01.dta", clear
replace inwyys = inwyr if essround<3
replace inwmms = inwmm if essround<3
replace inwdds = inwdd if essround<3
gen date = inwyys*10000 + inwmms * 100 +  inwdds
{ 
**Austria**
recode prtvtat (2 3=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_at1)
recode prtvtat (2 3=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_at2a)
recode prtvtat (2 6=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_at2b)
replace winlose_at2a=. if date<20050405
replace winlose_at2b=. if date>=20050405
recode prtvtaat (1 2=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_at3)
recode prtvtbat (1 2=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_at7)
recode prtvtbat (1 2=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_at8)
recode prtvtcat (2 3=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_at9)


**Belgium**
recode prtvtbe (1 3 5 11 13 14=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_be1)
recode prtvtabe (5 8 12 13=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_be2)
recode prtvtabe (5 8 12 13=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_be3)
recode prtvtbbe (2 8 9 12 13=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_be4)
recode prtvtcbe (2 9 12 13=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_be5)
recode prtvtcbe (2 5 8 9 12 13=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_be6)
recode prtvtcbe (3 2 8 12=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_be7)
recode prtvtcbe (3 2 8 12=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_be8)
recode prtvtdbe (2 3 8 12=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_be9a)
recode prtvtdbe (2 8 12=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_be9b)
replace winlose_be9a=. if date<20181209
replace winlose_be9b=. if date>=20181209


**Bulgaria**
recode prtvtbg (1 2 5=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_bg3)
recode prtvtabg (1 2 5=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_bg4)
recode prtvtbbg (1=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_bg5)
recode prtvtcbg (1=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_bg6a)
recode prtvtcbg (13=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_bg6b)
**Unsure about coding here - government seems to be party-independent so this is coded 13 "candidates raised by the initiative committees", according to Google Translate** 
replace winlose_bg6a=. if date<20081210
replace winlose_bg6b=. if date>=20081210
recode prtvtdbg (1 3=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_bg9)

**Switzerland**
recode prtvtch (1 2 3 4=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_ch1)
recode prtvtch (1 2 3 4=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_ch2)
recode prtvtach (1 2 3=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_ch3)
recode prtvtbch (1 2 3=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_ch4a)
recode prtvtbch (1 2 3 4 17=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_ch4b)
replace winlose_ch4a=. if date<20130313 
replace winlose_ch4b=. if date>=20130313  
recode prtvtcch (1 2 3 4 20=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_ch5)
recode prtvtdch (1 2 3 4 7=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_ch6)
recode prtvtech (1 2 3 4 7=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_ch7)
recode prtvtfch (1 2 3 4=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_ch8)
recode prtvtgch (1 2 3 4=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_ch9)

**Cyprus**
recode prtvtcy (1 2 7=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_cy3)
recode prtvtcy (1 2 7=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_cy4)
recode prtvtcy (1 2=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_cy5)
recode prtvtacy (1=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_cy6)
recode prtvtbcy (3 5=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_cy9)

**Czechia**
recode prtvtcz (1 10 14=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_cz1)
recode prtvtcz (1 10 14=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_cz2)
**ESS variable matches poorly with actual governments for first two rounds so the results might be misleading**
recode prtvtacz (6=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_cz4)
recode prtvtbcz (5 6 7=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_cz5)
recode prtvtccz (3 5 8=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_cz6)
recode prtvtdcz (2 4 6=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_cz7)
recode prtvtdcz (2 4 6=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_cz8)
recode prtvtecz (2 4=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_cz9)

**Germany first vote**
recode prtvde1 (1 3=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_de11)
recode prtvade1 (1 3=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_de12)
recode prtvbde1 (1 2=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_de13)
recode prtvbde1 (1 2=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_de14)
recode prtvcde1 (1 2=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_de15)
recode prtvdde1 (1 2=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_de16)
recode prtvede1 (1 2=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_de17)
recode prtvede1 (1 2=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_de18)
recode prtvede1 (1 2=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_de19)

**Germany second vote**
*recode prtvde2 (1 3=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_de21)
*recode prtvade2 (1 3=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_de22)
*recode prtvbde2 (1 2=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_de23)
*recode prtvbde2 (1 2=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_de24)
*recode prtvcde2 (1 2=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_de25)
*recode prtvdde2 (1 2=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_de26)
*recode prtvede2 (1 2=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_de27)
*recode prtvede2 (1 2=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_de28)
*recode prtvede2 (1 2=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_de29)

**Denmark**
recode prtvtdk (3 8=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_dk1)
recode prtvtdk (3 8=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_dk2)
recode prtvtadk (3 8=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_dk3)
recode prtvtbdk (3 7=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_dk4)
recode prtvtbdk (3 7=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_dk5)
recode prtvtcdk (1 2 4=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_dk6)
recode prtvtcdk (1 2=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_dk7)

**Estonia**
recode prtvtee (1 3 6=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_ee2)
recode prtvtaee (2 3 6=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_ee3a)
recode prtvtaee (1 3 4 5=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_ee3b)
replace winlose_ee3a=. if date<20070405
replace winlose_ee3b=. if date>=20070405
recode prtvtbee (1 3 5=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_ee4)
recode prtvtcee (1 3=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_ee5)
recode prtvtdee (1 3=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_ee6)
recode prtvteee (1 4=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_ee7)
recode prtvtfee (1 3 4=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_ee8a)
recode prtvtfee (2 3 4=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_ee8b)
replace winlose_ee8a=. if inwyr < 16 & inwmm < 11 & inwdd < 20
replace winlose_ee8b=. if inwyr > 16 & inwmm > 11 & inwdd > 20
recode prtvtgee (2 3 4=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_ee9)

**Spain**
recode prtvtes (1=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_es1)
recode prtvtaes (2=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_es2)
recode prtvtaes (2=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_es3)
recode prtvtbes (2=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_es4)
recode prtvtbes (2=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_es5)
recode prtvtces (1=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_es6)
recode prtvtces (1=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_es7)
recode prtvtdes (1=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_es8)

**Finland**
recode prtvtfi (1 2 9 10=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_fi1)
recode prtvtfi (2 4 9=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_fi2)
recode prtvtfi (2 4 9=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_fi3)
recode prtvtafi (1 2 4 7=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_fi4)
recode prtvtbfi (1 2 4 13=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_fi5)
recode prtvtcfi (1 2 5 12 13 14=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_fi6)
recode prtvtcfi (1 2 5 13=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_fi7)
recode prtvtdfi (1 3 4=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_fi8)
recode prtvtdfi (1 3 16=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_fi9)

**France**
recode prtvtfr (12 13=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_fr1)
recode prtvtfr (12 13=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_fr2)
recode prtvtafr (12 13=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_fr3)
recode prtvtbfr (6 11=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_fr4)
recode prtvtbfr (6 11=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_fr5)
recode prtvtcfr (7 9 12=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_fr6)
recode prtvtcfr (7 9=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_fr7)
recode prtvtcfr (7 9 12=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_fr8)
recode prtvtdfr (7 8 9 12=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_fr9)

**United Kingdom**
recode prtvtgb (2=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_gb1)
recode prtvtgb (2=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_gb2)
recode prtvtagb (2=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_gb3)
recode prtvtgb (2=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_gb4)
recode prtvtgb (1 3=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_gb5)
recode prtvtgb (1 3=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_gb6)
recode prtvtbgb (1 3=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_gb7a)
recode prtvtbgb (1=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_gb7b)
replace winlose_gb7a=. if inwyr < 15 & inwmm < 05 & inwdd < 08
replace winlose_gb7b=. if inwyr > 15 & inwmm > 05 & inwdd > 08
recode prtvtbgb (1=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_gb8)
recode prtvtcgb (1=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_gb9)


**Greece**
recode prtvtgr (1=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_gr1)
recode prtvtagr (2=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_gr2)
recode prtvtbgr (2=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_gr4a)
recode prtvtbgr (1=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_gr4b)
replace winlose_gr4a=. if date<20091006
replace winlose_gr4b=. if date>=20091006
recode prtvtcgr (1=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_gr5a)
recode prtvtcgr (1 2 4 8=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_gr5b)
replace winlose_gr5a=. if inwyr < 11 & inwmm < 11 & inwdd < 11
replace winlose_gr5b=. if inwyr > 11 & inwmm > 11 & inwdd > 11

**Croatia**
recode prtvthr (1 4 5 8=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_hr4)
recode prtvthr (1 4 8=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_hr5)

**Hungary**
recode prtvthu (5 7=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_hu1)
recode prtvthu (5 7=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_hu2)
recode prtvtahu (4 6=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_hu3)
recode prtvtbhu (4=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_hu4a)
recode prtvtbhu (4 16=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_hu4b)
replace winlose_hu4a=. if date<20090414
replace winlose_hu4b=. if date>=20090414
recode prtvtchu (1=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_hu5)
recode prtvtdhu (3=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_hu6)
recode prtvtehu (1=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_hu7)
recode prtvtehu (1=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_hu8)
recode prtvtfhu (3 5=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_hu9)

**Ireland**
recode prtvtie (1 4=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_ie1)
recode prtvtie (1 4=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_ie2)
recode prtvtie (1 4=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_ie3a)
recode prtvtie (1 4 5=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_ie3b)
replace winlose_ie3a=. if date<20070614
replace winlose_ie3b=. if date>=20070614
recode prtvtie (1 4 5=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_ie4)
recode prtvtaie (2 5=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_ie5)
recode prtvtaie (2 5=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_ie6)
recode prtvtaie (2 5=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_ie7)
recode prtvtbie (3 5=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_ie8)
recode prtvtcie (3 5=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_ie9)

**Israel - generally poor match between ESS coded alternatives and government parties. Could be inaccurate**
recode prtvtil (1 2 3 10 11=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_il1a)
recode prtvtil (2 3 10 11=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_il1b)
replace winlose_il1a=. if date<20020730
replace winlose_il1b=. if date>=20020730
recode prtvtail (1 3 4 8=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_il4)
recode prtvtbil (1 2 4 6 9=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_il5a)
recode prtvtbil (2 4 6 9 17=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_il5b)
replace winlose_il5a=. if date<20110117
replace winlose_il5b=. if date>=20111117
recode prtvtbil (2 3 4 6 9 17=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_il6)
recode prtvtcil (2 6=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_il7a)
recode prtvtcil (2 4 6 9=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_il7b)
replace winlose_il7a=. if date<20150514
replace winlose_il7b=. if date>=20150514
recode prtvtcil (2 4 6 9=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_il8)

**Iceland**
recode prtvtis (1 5=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_is2)
recode prtvtais (1 4=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_is6)
recode prtvtbis (2 3=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_is8a)
recode prtvtbis (1 3 12=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_is8b)

**Italy - many "other" here**
recode prtvtit (8 9 10 11=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_it1)
recode prtvtait (8 9 11 12 17=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_it2a)
recode prtvtait (3 4 7 17 19=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_it2b)
replace winlose_it2a=. if date<20060517
replace winlose_it2b=. if date>=20060517
recode prtvtbit (1 5 8 6 11=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_it6a)
recode prtvtbit (1 5 6 11 17=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_it6b)
replace winlose_it6a=. if date<20131118
replace winlose_it6b=. if date>= 20131118
recode prtvtbit (1 6 17=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_it8)
recode prtvtcit (7 8 14=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_it9)

**Lithuania fist vote**
recode prtvlt1 (5 9 11 14=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_lt15)
recode prtvalt1 (3 7 8 9=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_lt16)
recode prtvalt1 (3 7 8 9=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_lt17)
recode prtvblt1 (1 6=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_lt18)

**Lithuania second vote**
recode prtvlt2 (5 9 12 15=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_lt25)
recode prtvalt2 (3 7 8 9=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_lt26)
recode prtvalt2 (3 7 8 91=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_lt27)
recode prtvblt2 (1 7=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_lt28)

**Lithuania third vote**
recode prtvlt3 (5 9 12 15=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_lt35)
recode prtvalt3 (3 7 8 9=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_lt36)
recode prtvalt3 (3 7 8 9=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_lt37)
recode prtvblt3 (1 7=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_lt38)

**Luxemburg**
recode prtvtlu (1 3=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_lu1)
recode prtvtlu (1 2=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_lu2)

**Netherlands**
recode prtvtnl (1 3 4=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_nl1)
recode prtvtanl (1 3 5=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_nl2)
recode prtvtbnl (1 3=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_nl3a)
recode prtvtbnl (1 2 8=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_nl3b)
replace winlose_nl3a=. if date<20070222
replace winlose_nl3b=. if date>=20070222
recode prtvtcnl (1 2 8=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_nl4)
recode prtvtdnl (4 8=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_nl5a)
recode prtvtdnl (1 4=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_nl5b)
replace winlose_nl5a=. if date<20101014
replace winlose_nl5b=. if date>=20101014
recode prtvtenl (1 4=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_nl6a)
recode prtvtenl (1 2=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_nl6b)
replace winlose_nl6a=. if date<20121105
replace winlose_nl6b=. if date>=20121105
recode prtvtfnl (1 2=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_nl7)
recode prtvtfnl (1 2=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_nl8)
recode prtvtgnl (1 5 6 7=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_nl9)

**Norway*
recode prtvtno (4 5 7=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_no1)
recode prtvtno (4 5 7=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_no2)
recode prtvtno (2 3 6=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_no3)
recode prtvtno (2 3 6=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_no4)
recode prtvtano (2 3 6=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_no5)
recode prtvtano (2 3 6=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_no6)
recode prtvtbno (7 8=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_no7)
recode prtvtbno (7 8=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_no8)
recode prtvtbno (4 7 8=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_no9a)
recode prtvtbno (4 5 7 8=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_no9b)
replace winlose_no9a=. if date<20190122
replace winlose_no9b=. if date>=20190122 

**Poland**
recode prtvtpl (1 6=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_pl1)
recode prtvtpl (1 6=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_pl2)
recode prtvtapl (3 14 16=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_pl3)
recode prtvtbpl (3 5=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_pl4)
recode prtvtbpl (3 5=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_pl5)
recode prtvtcpl (2 5=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_pl6)
recode prtvtcpl (2 5=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_pl7)
recode prtvtdpl (6=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_pl8)
recode prtvtdpl (6=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_pl9)

**Portugal**
recode prtvtpt (2 11=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_pt1)
recode prtvtpt (2 11=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_pt2)
recode prtvtapt (11=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_pt3)
recode prtvtapt (11=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_pt4)
recode prtvtbpt (11=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_pt5)
recode prtvtbpt (2 10=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_pt6)
recode prtvtbpt (2 10=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_pt7a)
recode prtvtbpt (11=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_pt7b)
replace winlose_pt7a=. if date<20151126
replace winlose_pt7b=. if date>=20151126
recode prtvtcpt (11=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_pt8)

**Sweden**
recode prtvtse (6=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_se1)
recode prtvtse (6=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_se2)
recode prtvtse (6=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_se3a)
recode prtvtse (1 2 3 5=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_se3b)
replace winlose_se3a=. if date<20061005
replace winlose_se3b=. if date>=20061005
recode prtvtse (1 2 3 5=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_se4)
recode prtvtase (1 2 3 5=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_se5)
recode prtvtbse (1 2 3 5=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_se6)
recode prtvtbse (1 3 2 5=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_se7a)
recode prtvtbse (4 6=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_se7b)
replace winlose_se7a=. if date<20141003
replace winlose_se7b=. if date>=20141003
recode prtvtbse (4 6=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_se8)

**Slovenia**
recode prtvtsi (2 3 7=1) (66/max=.) (nonmissing=0) if essround==1, gen(winlose_si1)
recode prtvtasi (2 3 7=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_si2)
recode prtvtbsi (1 3 5 6=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_si3)
recode prtvtcsi (1 4 5 6=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_si4a)
recode prtvtcsi (1 2 8 9=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_si4b)
replace winlose_si4a=. if date<20081121
replace winlose_si4b=. if date>=20081121
recode prtvtcsi (1 2 8 9=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_si5)
recode prtvtdsi (1 3 4 5 8=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_si6)
recode prtvtesi (1 5 8=1) (66/max=.) (nonmissing=0) if essround==7, gen(winlose_si7)
recode prtvtesi (1 5 8=1) (66/max=.) (nonmissing=0) if essround==8, gen(winlose_si8)
recode prtvtfsi (1 3 5 6 10=1) (66/max=.) (nonmissing=0) if essround==9, gen(winlose_si9)

**Slovakia**
recode prtvtsk (2 4 6=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_sk2)
recode prtvtask (1 3 6=1) (66/max=.) (nonmissing=0) if essround==3, gen(winlose_sk3)
recode prtvtask (1 3 6=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_sk4)
recode prtvtbsk (2 4 5 6=1) (66/max=.) (nonmissing=0) if essround==5, gen(winlose_sk5)
recode prtvtcsk (3=1) (66/max=.) (nonmissing=0) if essround==6, gen(winlose_sk6)

**Turkey**
recode prtvttr (1=1) (66/max=.) (nonmissing=0) if essround==2, gen(winlose_tr2)
recode prtvtatr (1=1) (66/max=.) (nonmissing=0) if essround==4, gen(winlose_tr4)
egen winlose = rowmean(winlose_*)
replace winlose = 1 if winlose > 0 & winlose<1
}

pauze 
***************
* MAIN MODELS *
***************

*** recodes
recode inclusion (.=0)
gen countryyear = country*year

*** generate country-specific means of all variables 
foreach var of varlist nativism inclusion age female RRvote eduyrs winlose {
bys country: egen `var'_m = mean(`var')
}

*** generate demeaned versions of the two interacting variables
gen nativism_dm = nativism-nativism_m
gen inclusion_dm = inclusion-inclusion_m
gen RRvote_dm = RRvote-RRvote_m

*** generate interaction term for standard cFE and hybrid approach
gen ia_nativisminclusion_oo=nativism*inclusion

gen ia_RRvoteinclusion_oo=RRvote*inclusion

*** generate interaction terms for cFES with (extended) FE transformations
gen ia_nativisminclusion_do=nativism_dm*inclusion
gen ia_nativisminclusion_od=nativism*inclusion_dm
gen ia_nativisminclusion_dd=nativism_dm*inclusion_dm

gen ia_RRvoteinclusion_do=RRvote_dm*inclusion
gen ia_RRvoteinclusion_od=RRvote*inclusion_dm
gen ia_RRvoteinclusion_dd=RRvote_dm*inclusion_dm


*** generate interaction terms for cFES hybdrid models
gen ia_nativisminclusion_om=nativism*inclusion_m
gen ia_nativisminclusion_mo=nativism_m*inclusion 

gen ia_RRvoteinclusion_om=RRvote*inclusion_m
gen ia_RRvoteinclusion_mo=RRvote_m*inclusion 

*** generate interaction term for standard hybrid approach
bys country: egen ia_nativisminclusion_oo_m = mean(ia_nativisminclusion_oo)
bys country: egen ia_RRvoteinclusion_oo_m = mean(ia_RRvoteinclusion_oo)


*** LSDV approach - CFES models controlling for effect heterogeniety in inclusion and nativism
local DV "stfgov" // repeat all models for stfdem
reg `DV' c.nativism c.inclusion age female RRvote eduyrs winlose RRvote i.country if selection==1, cluster(country)
est store lsdv1
reg `DV' c.nativism##c.inclusion age female eduyrs winlose c.RRvote#c.inclusion i.country if selection==1, cluster(country)
est store lsdv2
reg `DV' c.nativism##c.inclusion age female eduyrs winlose c.RRvote##c.inclusion i.country i.country#c.nativism c.RRvote#i.country if selection==1, cluster(country)
est store lsdv3
reg `DV' c.nativism##c.inclusion age female  eduyrs winlose i.country i.country#c.inclusion c.RRvote#c.inclusion c.RRvote##i.country if selection==1, cluster(country)
est store lsdv4
reg `DV' c.nativism##c.inclusion age female  eduyrs winlose RRvote i.country i.country#c.nativism c.RRvote#c.inclusion i.country##c.inclusion RRvote##i.country if selection==1, cluster(country)
est store lsdv5
estout lsdv1 lsdv2 lsdv3 lsdv4 lsdv5 using "Tables\lsdv_`DV'.xls", replace keep(nativism inclusion c.nativism#c.inclusion) /// omit country fixed effects and slopes  
	cells(b(star fmt(%9.3f)) se(par(`"="("'`")""'))) 
	
*** cFES model with (extended) FE transformations 
xtreg `DV' c.nativism c.inclusion age female RRvote eduyrs winlose if selection==1, fe i(country) cluster(country)
estimates store fet1
xtreg `DV' c.nativism c.inclusion age female RRvote eduyrs winlose ia_nativisminclusion_oo ia_RRvoteinclusion_oo if selection==1, fe i(country) cluster(country)
estimates store fet2
xtreg `DV' c.nativism c.inclusion age female RRvote eduyrs winlose ia_nativisminclusion_od ia_RRvoteinclusion_od if selection==1, fe i(country) cluster(country)
estimates store fet3
xtreg `DV' c.nativism c.inclusion age female RRvote eduyrs winlose ia_nativisminclusion_do ia_RRvoteinclusion_do if selection==1, fe i(country) cluster(country)
estimates store fet4
xtreg `DV' c.nativism c.inclusion age female RRvote eduyrs winlose ia_nativisminclusion_dd ia_RRvoteinclusion_dd if selection==1, fe i(country) cluster(country)
estimates store fet5
estout fet1 fet2 fet3 fet4 fet5 using "Tables\fet_`DV'.xls", replace drop(age female RRvote eduyrs winlose ia_RRvoteinclusion_dd ia_RRvoteinclusion_do ia_RRvoteinclusion_od ia_RRvoteinclusion_oo) /// omit country fixed effects and slopes  
	cells(b(star fmt(%9.3f)) se(par(`"="("'`")""'))) 
	
*** cFES hybrid model 
local controls "age age_m female female_m winlose winlose_m  eduyrs eduyrs_m"
mixed `DV' c.nativism c.inclusion c.nativism_m c.inclusion_m `controls' if selection==1  || country: , mle var
estimates store hyb1
mixed `DV' c.nativism c.inclusion c.nativism_m c.inclusion_m ia_nativisminclusion_oo ia_nativisminclusion_oo_m `controls' RRvote RRvote_m ia_RRvoteinclusion_oo_m  if selection==1 [pweight=dweight] || country:  , mle var 
estimates store hyb2
mixed `DV' c.nativism c.inclusion c.nativism_m c.inclusion_m ia_nativisminclusion_oo ia_nativisminclusion_om `controls' RRvote RRvote_m ia_RRvoteinclusion_oo ia_RRvoteinclusion_om if selection==1 [pweight=dweight] || country: , mle var 
estimates store hyb3
mixed `DV' c.nativism c.inclusion c.nativism_m c.inclusion_m ia_nativisminclusion_oo ia_nativisminclusion_mo `controls' RRvote RRvote_m ia_RRvoteinclusion_oo ia_RRvoteinclusion_mo if selection==1 [pweight=dweight] || country: , mle var 
estimates store hyb4
mixed `DV' c.nativism c.inclusion c.nativism_m c.inclusion_m ia_nativisminclusion_oo ia_nativisminclusion_mo ia_nativisminclusion_om `controls' RRvote RRvote_m ia_RRvoteinclusion_oo ia_RRvoteinclusion_mo ia_RRvoteinclusion_om if selection==1 [pweight=dweight] || country: , mle var 
estimates store hyb5
est tab hyb1 hyb2 hyb3 hyb4 hyb5, star drop(`controls') // show models
estout hyb1 hyb2 hyb3 hyb4 hyb5 using "Tables\hyb_`DV'.xls", replace ///
	cells(b(star fmt(%9.3f)) se(par(`"="("'`")""'))) 

estout stfdem1 stfgov1 stfdem2 stfgov2 stfdem3 stfgov3 using "Main table.xls", replace

	
***********
* FIGURES *
***********

* PART 1: without reshaping

* Figure 4: Main interactions Based on LSDV model
reg stfgov c.nativism##c.inclusion age female  eduyrs winlose RRvote i.country i.country#c.nativism c.RRvote#c.inclusion c.RRvote##i.country if selection==1, cluster(country)
margins, at(inclusion=(0 1) nativism=(1.667 5 9)) force
marginsplot, scheme(plotplainblind) title("Satisfaction with government") x(inclusion) xlabel(0 "Exclusion" 1 "Inclusion", alternate) ylabel(3(1)8) ytitle("Satisfaction with...", size(small)) xtitle("") recastci(rarea) ciopts(color(%40)) xsize(5) name(stfgov, replace) legend(order(1 "Low nativism (5th pct)" 2 "Average nativism (50th pct)" 3 "High nativism (95th pct)") position(6))
reg stfdem c.nativism##c.inclusion age female  eduyrs winlose RRvote i.country i.country#c.nativism c.RRvote#c.inclusion c.RRvote##i.country if selection==1, cluster(country)
estimates store stfgov1
margins, at(inclusion=(0 1) nativism=(1.667 5 9)) force
marginsplot, scheme(plotplainblind) title("Satisfaction with democracy") x(inclusion) xlabel(0 "Exclusion" 1 "Inclusion", alternate) ylabel(3(1)8) ytitle("Satisfaction with...", size(small)) xtitle("") recastci(rarea) ciopts(color(%40)) xsize(5) name(stfdem, replace) legend(order(1 "Low nativism (5th pct)" 2 "Average nativism (50th pct)" 3 "High nativism (95th pct)") position(6))
graph combine stfdem stfgov, scheme(plotplainblind)
graph export "Graphs\Main interaction both LSDV.png", replace

* Trend graphs appendix
regress nativism i.year##i.country if selection==1 [pweight=dweight]
margins, over(year country) 
marginsplot, by(country) byopts(title("")) scheme(plotplainblind) xsize(7) recastci(rarea) ciopts(color(maroon%80)) ylabel(0(1)10) xtitle("") ytitle("Nativism scale") title("")
graph export "\Graphs\Figure A1.png", replace

regress nativist i.year##i.country if selection==1 [pweight=dweight]
margins, over(year country) 
marginsplot, by(country) byopts(title(""))  scheme(plotplainblind) xsize(7) recastci(rarea) ciopts(color(maroon%80)) xtitle("") title("") ylabel(0(0.2)1) ytitle("Agree immigrants undermine culture") title("")
graph export "\Graphs\Figure A2.png", replace

* PART II: with reshaping
keep if selection==1
rename (stfgov stfdem) (stf1 stf2)
gen id = _n
keep stf1 stf2 id nativism nativist imsmetn-impcntr nativist2 RRincountry RRvote  age female edu3 winlose year country inclusion selection dweight pspwght
reshape long stf, i(id) j(level)
lab define level 1 "Government" 2 "Democracy" 
lab values level level
set matsize 999

* Figure 1
local clist 1 2 7 8 11 12 13 14 20 23 24 29
foreach c of numlist `clist' {
	levelsof year if inclusion==1 & country==`c',  sep(" ") local(incl)
	display "`r(levels)'"
	display `incl'
	regress stf i.year##i.level if selection==1 & country==`c' [pweight=dweight]
	local f0: label country `c'
	margins, over(level year) nose
	marginsplot, legend(off) xlabel(2002(2)2018, alternate) x(year) xline(`incl' 999999, lpattern(solid) lwidth(5.5) lcolor(maroon%30)) scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) xtitle("") ytitle("")  title(`f0') legend(off) name(gr`c', replace)  nodraw
	}
graph combine gr1 gr2 gr7 gr8 gr11 gr12 gr13 gr14 gr20 gr23 gr24 gr29, scheme(plottig) xcommon ycommon 
graph export "\Graphs\Figure 1.png", replace

* Figure 2
local clist 1 2 7 8 11 12 13 14 20 23 24 29
foreach c of numlist `clist' {
	levelsof year if inclusion==1 & country==`c',  sep(" ") local(incl)
	display "`r(levels)'"
	display `incl'
	regress stf c.nativism##i.year##i.level if selection==1 & country==`c' [pweight=dweight]
	local f0: label country `c'
	margins, dydx(nativism) over(level year country) nose
	marginsplot, legend(off) xlabel(2002(2)2018, alternate) ylabel(-1(0.5)0.5) x(year) xline(`incl' 999999, lpattern(solid) lwidth(5.5) lcolor(maroon%30)) scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) xtitle("") ytitle("") yline(0) title(`f0') legend(off) name(gr`c', replace)  nodraw
	}
graph combine gr1 gr2 gr7 gr8 gr11 gr12 gr13 gr14 gr20 gr23 gr24 gr29, scheme(plottig) xcommon ycommon 
graph export "\Graphs\Figure 2.png", replace


* Figure 3
* - a
local clist 1 2 7 8 11 12 13 14 20 23 24 29
foreach c of numlist `clist' {
	levelsof year if inclusion==1 & country==`c',  sep(" ") local(incl)
	display "`r(levels)'"
	display `incl'
	regress stf c.nativism##i.year if selection==1 & level==1 & country==`c' [pweight=dweight]
	local f0: label country `c'
	margins, at(nativism=(1.667 9)) over(year) nose
	marginsplot,  legend(off) xlabel(2002(2)2018, alternate) x(year) xline(`incl' 999999, lpattern(solid) lwidth(5.5) lcolor(maroon%30)) scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) xtitle("") ytitle("") legend(off) title(`f0') name(gr`c', replace)  nodraw
	}
graph combine gr1 gr2 gr7 gr8 gr11 gr12 gr13 gr14 gr20 gr23 gr24 gr29, scheme(plottig) xcommon ycommon title("Satisfaction with government")
graph export "\Graphs\Figure 3a.png", replace

* - b
local clist 1 2 7 8 11 12 13 14 20 23 24 29
foreach c of numlist `clist' {
	levelsof year if inclusion==1 & country==`c',  sep(" ") local(incl)
	display "`r(levels)'"
	display `incl'
	regress stf c.nativism##i.year if selection==1 & level==2 & country==`c' [pweight=dweight]
	local f0: label country `c'
	margins, at(nativism=(1.667 9)) over(year) nose
	marginsplot, legend(off) xlabel(2002(2)2018, alternate) x(year) xline(`incl' 999999, lpattern(solid) lwidth(5.5) lcolor(maroon%30)) scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) xtitle("") ytitle("") title(`f0') legend(off) name(gr`c', replace)  nodraw 
	}
graph combine gr1 gr2 gr7 gr8 gr11 gr12 gr13 gr14 gr20 gr23 gr24 gr29, scheme(plottig) xcommon ycommon title("Satisfaction with democracy")
graph export "\Graphs\Figure 3b.png", replace
